/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.persistence.EntityManager;

import cn.com.chinasoa.course.subscribe.model.SubscribeOrder;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-4下午05:42:03
 */
public class SubscribeDocumentHelper {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			FileOutputStream out = new FileOutputStream("out.pdf");
			writeDocument(351l,out);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static void writeDocument(long orderId,OutputStream tOutputStream){
		Document document = new Document();
		try {
			PdfWriter.getInstance(document,tOutputStream);
			document.open();
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font FontChinese = new Font(bfChinese, 10, Font.NORMAL);
            
			EntityManager em = EntityManagerHelper.getEntityManager();
			SubscribeOrder tSubscribeOrder = em.find(SubscribeOrder.class,orderId);
			
			Image jpg = Image.getInstance(SubscribeDocumentHelper.class.getResource("logo.jpg"));
            document.add(jpg);
			
			Font titleFont = new Font(bfChinese, 24, Font.BOLD);
			Paragraph title = new Paragraph("报名回执", titleFont);
			title.setIndentationLeft(200);
			title.setSpacingAfter(10);
			document.add(title);
			
			if(tSubscribeOrder.isStudentSex()){
				Paragraph preTitle = new Paragraph(tSubscribeOrder.getStudentName()+" 女士:", FontChinese);
				document.add(preTitle);
			}else{
				Paragraph preTitle = new Paragraph(tSubscribeOrder.getStudentName()+" 先生:", FontChinese);
				document.add(preTitle);
			}

			Paragraph confirm = new Paragraph("您好，我们已经收到您预订的课程信息，请核对您选择的课程信息如下：", FontChinese);
			confirm.setIndentationLeft(40);
			document.add(confirm);
			
			Table table = new Table(2);
			table.setWidth(80);
			table.setWidths(new float[]{1,4});
			table.setPadding(2);
			
			Cell cell11 = new Cell(new Paragraph("订单号码:", FontChinese));
			Cell cell12 = new Cell(orderId+"");
			table.addCell(cell11);
			table.addCell(cell12);
			
			Cell cell21 = new Cell(new Paragraph("学员姓名:", FontChinese));
			Cell cell22 = new Cell(new Paragraph(tSubscribeOrder.getStudentName(), FontChinese));
			table.addCell(cell21);
			table.addCell(cell22);
			
			Cell cell31 = new Cell(new Paragraph("学员性别:", FontChinese));
			Cell cell32 = new Cell(new Paragraph(DataTypeUtil.renderSex(tSubscribeOrder.isStudentSex()), FontChinese));
			table.addCell(cell31);
			table.addCell(cell32);

			Cell cell41 = new Cell(new Paragraph("电子邮件:", FontChinese));
			Cell cell42 = new Cell(new Paragraph(tSubscribeOrder.getStudentEmail(), FontChinese));
			table.addCell(cell41);
			table.addCell(cell42);

			Cell cell51 = new Cell(new Paragraph("工作单位:", FontChinese));
			Cell cell52 = new Cell(new Paragraph(tSubscribeOrder.getStudentCompany(),FontChinese));
			table.addCell(cell51);
			table.addCell(cell52);
			
			Cell cell61 = new Cell(new Paragraph("联系地址:", FontChinese));
			Cell cell62 = new Cell(new Paragraph(tSubscribeOrder.getStudentAddress(),FontChinese));
			table.addCell(cell61);
			table.addCell(cell62);
			
			Cell cell71 = new Cell(new Paragraph("联系电话:", FontChinese));
			Cell cell72 = new Cell(new Paragraph(tSubscribeOrder.getStudentPhone(),FontChinese));
			table.addCell(cell71);
			table.addCell(cell72);
			
			Cell cell81 = new Cell(new Paragraph("移动电话:", FontChinese));
			Cell cell82 = new Cell(new Paragraph(tSubscribeOrder.getMobilePhone(),FontChinese));
			table.addCell(cell81);
			table.addCell(cell82);
			
			Cell cell91 = new Cell(new Paragraph("所选课程:", FontChinese));
			Cell cell92 = new Cell(new Paragraph(tSubscribeOrder.getArrangedCourse().getTrainingSubject().getCode(),FontChinese));
			table.addCell(cell91);
			table.addCell(cell92);
			
			if(tSubscribeOrder.getPrivilegeStrategy()!=null){
				Cell cell101 = new Cell(new Paragraph("付费金额:", FontChinese));
				Cell cell102 = new Cell(new Paragraph(tSubscribeOrder.getActualPrice()+"元(原价"+tSubscribeOrder.getArrangedCourse().getTrainingSubject().getPrice()+"元)"+":"+tSubscribeOrder.getPrivilegeStrategy().getDescription(),FontChinese));
				table.addCell(cell101);
				table.addCell(cell102);
			}else{
				Cell cell101 = new Cell(new Paragraph("付费金额:", FontChinese));
				Cell cell102 = new Cell(new Paragraph(tSubscribeOrder.getActualPrice()+"元",FontChinese));
				table.addCell(cell101);
				table.addCell(cell102);
			}
			Cell cell111 = new Cell(new Paragraph("授课城市:", FontChinese));
			Cell cell112 = new Cell(new Paragraph(tSubscribeOrder.getArrangedCourse().getTrainingAddress().getCity(),FontChinese));
			table.addCell(cell111);
			table.addCell(cell112);
			
			Cell cell121 = new Cell(new Paragraph("授课地址:", FontChinese));
			Cell cell122 = new Cell(new Paragraph(tSubscribeOrder.getArrangedCourse().getTrainingAddress().getAddress(),FontChinese));
			table.addCell(cell121);
			table.addCell(cell122);
			
			Cell cell131 = new Cell(new Paragraph("授课日期:", FontChinese));
			Cell cell132 = new Cell(new Paragraph(DataTypeUtil.renderCalenderCN(tSubscribeOrder.getArrangedCourse().getStartDate())+"-"+DataTypeUtil.renderCalenderCN(tSubscribeOrder.getArrangedCourse().getEndDate()),FontChinese));
			table.addCell(cell131);
			table.addCell(cell132);
			
			Cell cell141 = new Cell(new Paragraph("授课时间:", FontChinese));
			Cell cell142 = new Cell(new Paragraph(DataTypeUtil.renderTimeAMPM(tSubscribeOrder.getArrangedCourse().getTrainingSubject().getStartTime())+"-"+DataTypeUtil.renderTimeAMPM(tSubscribeOrder.getArrangedCourse().getTrainingSubject().getEndTime()),FontChinese));
			table.addCell(cell141);
			table.addCell(cell142);
			
			document.add(table);
			
			Paragraph noticeA = new Paragraph("关于课程的详细信息请访问：http://www.chinasoa.com.cn/cnsoa/portal/courseActivities", FontChinese);
			noticeA.setIndentationLeft(40);
			document.add(noticeA);
			
			Paragraph noticeB = new Paragraph("授课时请自带笔记本电脑，如不方面携带，请提前与我们联系，我们将为您安排授课专用计算机。", FontChinese);
			noticeB.setIndentationLeft(40);
			document.add(noticeB);
			noticeB.setSpacingAfter(10);
			
			Font payFont = new Font(bfChinese, 20, Font.BOLD);
			
			Paragraph noticePay = new Paragraph("付款信息", payFont);
			noticePay.setIndentationLeft(200);
			document.add(noticePay);
			
			Paragraph noticePayInfo = new Paragraph("请安排付款至下面的帐号：", FontChinese);
			noticePayInfo.setIndentationLeft(40);
			document.add(noticePayInfo);

			Paragraph noticePayInfoA = new Paragraph("户名：XXXXXXXXXXXXXXXXX", FontChinese);
			noticePayInfoA.setIndentationLeft(40);
			document.add(noticePayInfoA);

			Paragraph noticePayInfoB = new Paragraph("开户银行：XXXXXXXXXXXXXXXXX", FontChinese);
			noticePayInfoB.setIndentationLeft(40);
			document.add(noticePayInfoB);

			Paragraph noticePayInfoC = new Paragraph("帐号：XXXXXXXXXXXXXXXXX", FontChinese);
			noticePayInfoC.setIndentationLeft(40);
			noticePayInfoC.setSpacingAfter(10);
			document.add(noticePayInfoC);

			
			Paragraph noticePayInfoD = new Paragraph("付款后，请以EMail或电话通知我，确认付款后将立刻为您安排课程。", FontChinese);
			noticePayInfoD.setIndentationLeft(40);
			document.add(noticePayInfoD);
			
			Paragraph noticePayInfoS = new Paragraph("您随时可以在网上查询您的订单处理情况。", FontChinese);
			noticePayInfoS.setIndentationLeft(40);
			noticePayInfoS.setSpacingAfter(30);
			document.add(noticePayInfoS);
			

			
			Paragraph noticePayInfoE = new Paragraph("中流科技", FontChinese);
			noticePayInfoE.setIndentationLeft(320);
			document.add(noticePayInfoE);

			
			Paragraph noticePayInfoF = new Paragraph("联系人: Candy ", FontChinese);
			noticePayInfoF.setIndentationLeft(320);
			document.add(noticePayInfoF);

			
			Paragraph noticePayInfoG = new Paragraph("电话：010-XXXXXX", FontChinese);
			noticePayInfoG.setIndentationLeft(320);
			document.add(noticePayInfoG);

			
			Paragraph noticePayInfoH = new Paragraph("E-mail：candy@chinasoa.com.cn", FontChinese);
			noticePayInfoH.setIndentationLeft(320);
			document.add(noticePayInfoH);
			
			Paragraph noticePayInfoW = new Paragraph("http://www.chinasoa.com.cn", FontChinese);
			noticePayInfoW.setIndentationLeft(320);
			document.add(noticePayInfoW);
			
		} catch (DocumentException de) {
			System.err.println(de.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
		} 
		// step 5: we close the document
		document.close();
	}
}
